include_directories(.)

#
add_library(chainsaw SHARED
    $<TARGET_OBJECTS:common_obj>
    $<TARGET_OBJECTS:html_obj>
)

#
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

#
target_include_directories(chainsaw PUBLIC
    ${PROJECT_SOURCE_DIR}/src
)

find_package(Threads REQUIRED)
target_link_libraries(chainsaw PRIVATE Threads::Threads)

add_subdirectory(common)
add_subdirectory(html)

# install module 
set(MODULES common html safe_struct)
foreach(module ${MODULES})
    install_target_and_headers(chainsaw ${module})
endforeach()

# install top head-file
install(DIRECTORY ${PROJECT_SOURCE_DIR}/src/
        DESTINATION ${INSTALL_INCLUDE_DIR}
        FILES_MATCHING 
        PATTERN "*.h" PATTERN "*.hpp" PATTERN "chainsaw_define"
        PATTERN "*/" EXCLUDE   
        PATTERN "cmake" EXCLUDE 
)

# install cmake
install(FILES cmake/ChainsawConfig.cmake
    DESTINATION ${INSTALL_LIB_DIR}/
)

configure_file(version.h.in ${GENERATE_DIR}/chainsaw-version.h @ONLY)
install(FILES ${GENERATE_DIR}/chainsaw-version.h DESTINATION ${INSTALL_INCLUDE_DIR})

configure_file(chainsaw.h.in ${GENERATE_DIR}/chainsaw.h)
install(FILES ${GENERATE_DIR}/chainsaw.h DESTINATION ${INSTALL_INCLUDE_DIR})

add_custom_target(
    generate_tasks ALL
    COMMENT "Generating version header file: ${GENERATED_DIR}/version.h"
    ${GENERATE_DIR}/chainsaw-version.h
    ${GENERATE_DIR}/chainsaw.h
)

add_library(chainsaw-heads INTERFACE)
target_link_directories(chainsaw-heads INTERFACE ${PROJECT_SOURCE_DIR}/src)

if(WIN32)
    set_target_properties(chainsaw PROPERTIES
        WINDOWS_EXPORT_ALL_SYMBOLS ON
    )
endif()